# 与数据库进行交互，获取数据库中的表，属性，属性下的所有值


import pymysql
from .basic.config import *



def mysql_getAllTableName():
    db = pymysql.connect(**config)

    cursor = db.cursor()

    # 获取数据库中的所有的表
    sql = """
    select table_name from information_schema.tables where table_schema='%s'
    """ % databaseName

    cursor.execute(sql)
    result = cursor.fetchall()
    list = []
    for i in result:
        list.append(i[0])

    db.commit()
    cursor.close()
    db.close()

    return list



def mysql_get_attributes_oneTable(tableName):
    db = pymysql.connect(**config)

    cursor = db.cursor()
    # 获取表中的属性 sql = "desc %s" % tableName
    sql = """
    desc %s
    """ % tableName

    cursor.execute(sql)
    result = cursor.fetchall()
    list = []
    for i in result:
        list.append(i[0])

    print(list)
    db.commit()
    cursor.close()
    db.close()

    return list

def mysql_get_allvalues_oneAttribute(tableName,attributeName):
    db = pymysql.connect(**config)

    cursor = db.cursor()
    sql = """
    select %s
    from %s
    """ % (attributeName,tableName)

    cursor.execute(sql)
    result = cursor.fetchall()

    count = 1
    list =[]

    # 此处不知道为啥是三倍
    for i in range(0,int(len(result)/3)):
        # print( count , ' ' ,i)
        # count = count+1
        list.append(result[i][0])

    db.commit()
    cursor.close()
    db.close()

    return list